package com.zhangyujun.bysj.manager.utils;

import com.zhangyujun.bysj.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        //sysMenuList所有菜单集合
        //创建list集合，用于封装最终的数据
        List<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getParentId() == 0) {
                //找到顶层的菜单，条件parentId = 0
                trees.add(findChildren(sysMenu, sysMenuList));
            }
        }
        return trees;
    }

    //遍历子节点
    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {
        //SysMenu 有属性 private List<SysMenu> Children; 封装下一层数据
        sysMenu.setChildren(new ArrayList<>());
        //进行递归调用
        for (SysMenu menu : sysMenuList) {
            if (menu.getParentId() == sysMenu.getId()) {
                sysMenu.getChildren().add(findChildren(menu, sysMenuList));
            }
        }
        return sysMenu;
    }
}
